import 'package:dio/dio.dart';
import 'package:dio/io.dart';

import '../../config/const.dart';
import '../interceptor/result.dart';
import '../interceptor/sign.dart';

/// 基于自定义验签规则的，接口请求工具类
class ApiClientManager {
  factory ApiClientManager() =>_getInstance();
  static ApiClientManager get instance => _getInstance();
  static ApiClientManager? _instance;
  // static List<String> postPaths=[];
  // static List<String> putPaths=[];
  // static List<String> deletePaths=[];
  Dio? _dio;
  Dio dio() {
    return _dio!;
  }

  ApiClientManager._internal() {
    // 初始化
    if (null == _dio) {
      _dio = Dio();
      _dio!.options.baseUrl = baseURL;
      _dio!.options.connectTimeout = const Duration(seconds: 60);
      _dio!.options.receiveTimeout = const Duration(seconds: 60);
      // 添加签名拦截器
      _dio!.interceptors.add(SignInterceptor());
      _dio!.interceptors.add(ResultInterceptor());
      (_dio!.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client){
        client.badCertificateCallback = (cert,host,port){
          return true;
        };
      };
    }
  }
  static ApiClientManager _getInstance() {
    _instance ??=  ApiClientManager._internal();
    return _instance!;
  }

}
